package com.jsshse.controller.report;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jsshse.service.report.RBaseServiceI;
import com.jsshse.util.Pages;


@Controller
@RequestMapping("/report/rBase/*")
public class RBaseController {
	

	@Resource
	private RBaseServiceI rBaseService;
	
	private static Logger log = Logger.getLogger(RBaseController.class);
	
	@RequestMapping(value = "/save")
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String data = request.getParameter("data");
		log.debug(data);
		JSONObject json = JSONObject.fromObject(data);
		HashMap m= (HashMap)JSONObject.toBean(json,HashMap.class);
		
		String id=m.get("r_id").toString();
		log.debug(id);
		if(id.length()>0){
			rBaseService.update(m);
		}else{
			rBaseService.add(m);
		}

		
		JSONObject jsonObject = JSONObject.fromObject(m);      
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/del")
	public ModelAndView romove (HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String idStr = request.getParameter("id")==null?"":request.getParameter("id"); 
		 log.debug("id:"+idStr); 
		 if(idStr.length()>0){
			 String[] ids = idStr.split(",");
			    for (int i = 0, l = ids.length; i < l; i++)
			    {
			        rBaseService.delete(ids[i]);
			    }  
		 } 
		return null;
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public ModelAndView get(@RequestParam("id") String id,HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map map = rBaseService.queryById(id);
		JSONObject jsonObject = JSONObject.fromObject(map); 
		log.debug(jsonObject.toString()); 
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/list")
	public ModelAndView listAll (HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String reportID = request.getParameter("reportID")==null?"":request.getParameter("reportID");        
 
	    int pageIndex = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));        
//		String sortField = request.getParameter("sort");
//		String sortOrder = request.getParameter("order");

	    StringBuffer sb=new StringBuffer();
	    Pages pages=new Pages();
	    
	    if(reportID.length()>0){
	    	sb.append(" AND r_id ='"+reportID+"'");
	    }
    
//	    pages.setSortField(sortField);
//	    pages.setSortOrder(sortOrder);
	    pages.setQueryCondition(sb.toString());
	    pages.setPageSize(pageSize);
	    pages.setPageStart((pageIndex-1)*pageSize);
	    
	    pages = rBaseService.queryForList(pages);
	    
	   HashMap result=new HashMap();
	    result.put("rows",pages.getDataList());
        result.put("total",pages.getTotal());
       
        JSONObject jsonObject = JSONObject.fromObject(result);      
        response.getWriter().write(jsonObject.toString());
		return null;
	}
	@RequestMapping("/reportData")
	public ModelAndView reportData (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String reportID = request.getParameter("reportID")==null?"":request.getParameter("reportID");        
		String params = request.getParameter("params")==null?"{}":request.getParameter("params");        
		
		int pageIndex = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));        
//		String sortField = request.getParameter("sort");
//		String sortOrder = request.getParameter("order");
		
		log.debug(params);
		JSONObject json = JSONObject.fromObject(params);
		HashMap paramQuery= (HashMap)JSONObject.toBean(json,HashMap.class);
		
		StringBuffer sb=new StringBuffer();
		Pages pages=new Pages();
		Map paramMap=new HashMap();
		if(reportID.length()>0){
			sb.append(" AND r_id ='"+reportID+"'");
			paramMap.put("r_id", reportID);
		}
		paramMap.put("paramQuery", paramQuery);
		
//	    pages.setSortField(sortField);
//	    pages.setSortOrder(sortOrder);
		pages.setQueryCondition(sb.toString());
		pages.setPageSize(pageSize);
		pages.setPageStart((pageIndex-1)*pageSize);
		pages.setQueryParam(paramMap);
		pages = rBaseService.reportData(pages);
		
		HashMap result=new HashMap();
		result.put("rows",pages.getDataList());
		result.put("total",pages.getTotal());
		
		JSONObject jsonObject = JSONObject.fromObject(result);      
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/reportInit")
	public ModelAndView reportInit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String reportID = request.getParameter("reportID")==null?"":request.getParameter("reportID");  
		Map m=rBaseService.reportInit(reportID);
		log.debug(m);
		request.setAttribute("tb",m);
		return new ModelAndView("report/rBaseList");
	}
 
}
